import Reflux from 'reflux';
import PageConst from './PageConst';
import Actions from './Actions';
import update from 'react-addons-update';
import nattyStorage from 'natty-storage';
import DB from '../../../app/db';
const { AMAP_WEB_KEY } = require(`./../../../config/${ENV}.json`);

export default Reflux.createStore({
  listenables: [Actions],
  data: {
    ...PageConst,
    city: "宜昌",
    iosShow: false,
    auto_show: false,
    showLoading: true,
    menus: [{
      label: 'PM2.5',
      onClick: ()=> {
        Actions.orderByPm()
      }
    }, {
      label: '噪音',
      onClick: ()=> {
        Actions.orderByZaoyin()
      }
    }],
    act: [
      {
        label: '取消',
        onClick: ()=> {
          Actions.closeActionSheet()
        }
      }
    ],
    searchDivShow: false,
    searchData: [],
    position: {
      longitude: 111.3007521629,
      latitude: 30.7085782898
    },
    affect: "",
    city: "",
    pm25: "",
    quality: "",
    measure: "",
    projectOrder: [],
    positionResult: {}
  },

  onShowActionSheet: function () {
    this.data.iosShow = update(this.data.iosShow, {$set: true});
    this.updateComponent();
  },

  onCloseActionSheet: function () {
    this.data.iosShow = update(this.data.iosShow, {$set: false});
    this.data.auto_show = update(this.data.auto_show, {$set: false});
    this.updateComponent();
  },

  onHandleChange: function (text) {
    text = text.replace(/(^\s+)|(\s+$)/g, "")
    if ("" != text && null != text) {
      let that = this;
      DB.WechatAPI.seachConstruction({
        pname: text
      }).then(
        function (content) {
          that.data.searchDivShow = update(that.data.searchDivShow, {$set: true});
          if (content.total > 0) {

            that.data.searchData = update(that.data.searchData, {$set: content.rows});

          } else {

            that.data.searchData = update(that.data.searchData, {$set: []});

          }

        },
        function (error) {
          console.log(error);
        }
      );
      that.updateComponent();
    }

  },

  onHandleCancel: function () {
    this.data.searchDivShow = update(this.data.searchDivShow, {$set: false});
    this.updateComponent();
  },

  onToDistrictDetail: function (Control) {
    Control.go('/lsgdhome/lsgdjcDetail', {city: this.data.city});
  },

  onOrderByPm: function () {
    this.onLoadData("pmten", "asc");
    this.onCloseActionSheet();
  },

  onOrderByZaoyin: function () {
    this.onLoadData("tsp", "asc");
    this.onCloseActionSheet();
  },


  onLoadingControl: function (obj) {
    this.data.showLoading = update(this.data.showLoading, {$set: obj});
    this.updateComponent();
  },

  onLoadWeather: function (orderElement, orderDeriction) {
    let that = this;
    DB.WechatAPI.queryWeatherInfo({
      city: that.data.positionResult.addressComponent.city,
    }).then(
      function (content) {
        console.info(content);
        that.data = update(that.data, {
            city: {$set: content.city},
            pm25: {$set: content.pm25},
            quality: {$set: content.quality},
            affect: {$set: content.affect},
            city: {$set: that.data.positionResult.addressComponent.city}
          }
        );
        that.onLoadData(orderElement, orderDeriction);
      },
      function (error) {
        console.log(error);
        that.onLoadingControl(false);
      }
    );
  },

  onLoadData: function (orderElement, orderDeriction) {
    let that = this;
    //let district=positon.addressComponent.district
    DB.WechatAPI.queryProjectOrder({
      city: that.data.positionResult.addressComponent.city,
      cityCode: that.data.positionResult.addressComponent.citycode,
      orderElement: orderElement, //tsp or pmtwo
      orderDeriction: orderDeriction
    }).then(
      function (content) {
        console.info(content);
        that.data.projectOrder = update(that.data.projectOrder, {$set: content.projectOrder});
        that.onLoadingControl(false);
      },
      function (error) {
        console.log(error);
        that.onLoadingControl(false);
      }
    );
    that.onLoadingControl(false);
  },

  onSetPositionResult: function (result) {

    let that = this;

    DB.WechatAPI.getGdPostion({
      output: 'json',
      location: result.longitude + ',' + result.latitude,
      key: AMAP_WEB_KEY, //tsp or pmtwo
      radius: 10,
      extensions: 'base'
    }).then(
      function (content) {
        that.data.positionResult = update(that.data.positionResult, {$set: content.regeocode});
        that.updateComponent();
        that.onLoadWeather("pmten", "asc");
      },
      function (error) {
        console.log(error);
      }
    );


  },

  onFetch: function (params, cb) {
    let t = this;
    t.updateComponent();
    cb && cb(t.data);
  },

  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
